package cn.zebra.dev.product.module.collect.service.impl;

import cn.zebra.dev.ibatis.plus.service.impl.ServiceImpl;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.ibatis.plus.utils.Query;
import cn.zebra.dev.product.module.collect.dao.MemberProductCollectDao;
import cn.zebra.dev.product.module.collect.entity.MemberProductCollectEntity;
import cn.zebra.dev.product.module.collect.service.MemberProductCollectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author runnable@sina.cn
 */
@Service
public class MemberProductCollectServiceImpl extends ServiceImpl<MemberProductCollectDao, MemberProductCollectEntity> implements MemberProductCollectService {
    /**
     * 保存商品收藏
     *
     * @param memberId  用户id
     * @param productId 商品id
     * @return 保存成功返回True 保存失败返回False
     */
    @Override
    public Boolean saveCollect(Long memberId, Long productId) {
        MemberProductCollectEntity entity = this.findByMemberIdAndProductId(memberId, productId);
        if (entity == null) {
            entity = new MemberProductCollectEntity();
            entity.setDeleteFlag(false);
            entity.setCreateTime(LocalDateTime.now());
            entity.setMemberId(memberId);
            entity.setProductId(productId);
            return save(entity);
        }
        return false;
    }

    /**
     * 根据用户id和商品id查询用户商品收藏
     *
     * @param memberId  用户id
     * @param productId 商品id
     * @return 用户商品收藏对象
     */
    @Override
    public MemberProductCollectEntity findByMemberIdAndProductId(Long memberId, Long productId) {
        QueryWrapper<MemberProductCollectEntity> wrapper = new QueryWrapper<>();
        wrapper.apply("member_id = {0}", memberId);
        wrapper.apply("product_id = {0}", productId);
        wrapper.apply("delete_flag = FALSE");
        return this.getOne(wrapper);
    }

    /**
     * 根据主键id和用户id逻辑删除用户商品收藏
     *
     * @param id       主键id
     * @param memberId 用户id
     * @return 逻辑删除成功返回True 逻辑删除失败返回False
     */
    @Override
    public Boolean deleteByIdAndMemberId(Long id, Long memberId) {
        MemberProductCollectEntity entity = new MemberProductCollectEntity();
        entity.setDeleteFlag(true);
        UpdateWrapper<MemberProductCollectEntity> wrapper = new UpdateWrapper<>();
        wrapper.apply("member_id = {0}", memberId);
        wrapper.apply("id = {0}", id);
        wrapper.apply("delete_flag = FALSE");
        return this.update(entity, wrapper);
    }

    /**
     * 根据查询参数查询分页用户商品收藏信息
     *
     * @param params - key(查询参数)
     *               memberId   用户id
     *               productId  商品id
     *               deleteFlag 是否删除
     * @return 用户商品收藏分页信息
     */
    @Override
    public PageUtils<MemberProductCollectEntity> findPage(Map<String, Object> params) {
        QueryWrapper<MemberProductCollectEntity> wrapper = new QueryWrapper<>();
        wrapper.select("id, product_id, member_id");
        wrapper.apply(params.containsKey("memberId"), "member_id = {0}", params.get("memberId"));
        wrapper.apply(params.containsKey("productId"), "product_id = {0}", params.get("productId"));
        wrapper.apply(params.containsKey("deleteFlag"), "delete_flag = {0}", params.get("deleteFlag"));
        wrapper.orderByDesc("id");
        IPage<MemberProductCollectEntity> iPage = this.page(
                new Query<MemberProductCollectEntity>(params).getPage(),
                wrapper
        );
        return new PageUtils<>(iPage);
    }
}
